import { urls } from "./utils-global"
import { h } from 'vue'
import { ElMessage } from "element-plus"
const enum_code = {
    RP_RQ: { msg: '加载中...', tips() { ElMessage({ message: h('p', null, [h('i', { class: 'el-icon-loading' }, ''), h('font', { style: 'padding-left:15px;color:#989ca2' }, '加载中...')]), customClass: 'tips_loading', duration: 200000, center: true }); } },
    RP_OK: { msg: '请求成功', tips() { ElMessage.success({ message: `${this.msg}`, center: true, duration: 1000 }); } },
    RP_OK_GET: { msg: '获取数据成功', tips() { ElMessage.success({ message: `${this.msg}`, center: true, duration: 1000 }); } } , // 查询成功
    RP_OK_SET: { msg: '操作完成', tips() { ElMessage.success({ message: `${this.msg}`, center: true, duration: 1000 }); } } , // 操作成功
    RP_ERROR_GET: { msg: '获取数据失败', tips() { ElMessage.error({ message: `${this.msg}`, center: true, duration: 1000 }); } } , // 查询失败
    RP_ERROR_SET: { msg: '操作失败', tips() { ElMessage.error({ message: `${this.msg}`, center: true, duration: 1000 }); } },
    RP_ERROR: { msg: '请求错误', tips() { ElMessage.error({ message: `${this.msg}` }); } },
    RP_ERROR_PARA: { msg: '传入的参数有误', tips() { ElMessage.error({ message: `${this.msg}` }); } },
    RP_OBJNULL: { msg: '请求成功', tips() { } },
    RP_NULL: { msg: '', tips() { } },
    ERROR_400: { msg: '服务器请求失败', tips() { ElMessage.error({ message: `${this.msg}`, duration: 5000 }); } },
    ERROR_404: { msg: '链接请求失败', tips() { ElMessage.error({ message: `${this.msg}`, duration: 5000 }); } },
    ERROR_403: { msg: '没有权限访问', tips() { ElMessage.error({ message: `${this.msg}`, duration: 5000 }); } },
    ERROR_500: { msg: '服务请求错误', tips() { ElMessage.error({ message: `${this.msg}`, duration: 5000 }); } },
    ERROR_UNKNOWN: { msg: '未知错误', tips() { } },
    ERROR_100: { msg: '登录成功', tips() { ElMessage.success({ message: `${this.msg}`, duration: 1500 }); } },
    undefined: { msg: '传回的参数格式不正确', tips() { ElMessage.error({ message: `${this.msg}`, duration: 1500 }); } },
}

import axios from 'axios'
import {CONF} from "./config"
function getHttp() {
    if (CONF.BSType == 'webserver') {
        return axios.create({ baseURL: CONF.ServerUrl, headers: {'Content-Type': 'application/x-www-form-urlencoded'}, timeout: 10000 })
        // return axios.create({ baseURL: 'http://localhost:8090/', timeout: 2000 })
    }
    
    if (CONF.BSType == 'client') {
        return axios.create({ baseURL: CONF.ClientUrl, headers: {'Content-Type': 'application/x-www-form-urlencoded'}, timeout: 2000 })
    }
}
let loading = { isload: true, istips: true }
const http = getHttp()
let removeLoad = (className) => {
    var ele = document.getElementsByClassName(className)
    if(ele && ele.length !=0 )  [ele].forEach(item => { item[0].remove() });
}
import { userInfo } from "./utils-global"
http.interceptors.request.use(config => {
    console.log('----interceptors.request-----')
    config.url = urls[config.url]
    removeLoad('el-message')
    if (loading.isload)  enum_code.RP_RQ.tips() //加载标记
    const sessionkey = userInfo.UHID  //SISSIONKEY, UHID 测试试用
    if(sessionkey) config.headers.sessionkey = sessionkey
    return config
}, err => {
    
    return Promise.reject(err)
})

http.interceptors.response.use(res => {
    // console.log('----interceptors.response-----')
    //移除加载状态
    removeLoad('tips_loading');
    if (loading.istips) enum_code[res.data.code].tips()
    else loading.istips = true
    return res;
}, err => {
    removeLoad('tips_loading');
    enum_code.ERROR_400.tips()
    return Promise.reject(err)
})
export { http, loading as hloading }

